/*
 * dataTable.js
 * 
 * A collection of functions for displaying and manipulating the summary data table that is at
 * the center of this admissions tool
 *
 * AUTHOR
 *  PREBEN INGVALDSEN
 *    
 */

var cs441ShibeDoge = cs441ShibeDoge || {};

/*
 * showTables
 * 
 * Displays the summary data tables
 * 
 */
cs441ShibeDoge.showTables = function() {
	var tableContainer = document.getElementById("summaryDataTableContainer");
	tableContainer.style.display = "block";
	var loadingMessage = document.getElementById("loadingMessage");
	loadingMessage.style.display = "none";
}

/*
 * populateTable
 * 
 * Based on information in the dataObject that it receives as a parameter, populates the
 * dataTable with various pieces of data, hiding any columns if necessary
 * 
 * PARAMS
 * 	dataObject - An object containing all the pieces of data with which to populate
 * 				 the table, along with the name of the region to be populated
 * 				 and the type of region selected
 */
cs441ShibeDoge.populateTable = function(dataObject) {
	
	//Parse the data cells into HTML strings and add them into the DOM
	var dataTable1Data = "";
	dataTable1Data += "<td>" + dataObject.SAT_VERBAL + "</td>";
	dataTable1Data += "<td>" + dataObject.SAT_MATH + "</td>";
	dataTable1Data += "<td>" + dataObject.GPA + "</td>";
	dataTable1Data += "<td>" + dataObject.NUM_APPLICANTS + "</td>";
	dataTable1Data += "<td>" + dataObject.NUM_ACCEPTED + "</td>";
	dataTable1Data += "<td>" + dataObject.NUM_ENROLLED + "</td>";

	var dataTable1DataRow = document.getElementById("dataRow1");
	dataTable1DataRow.innerHTML = dataTable1Data;

	var dataTable2Data = "";
	dataTable2Data += "<td>" + dataObject.RELIGION + "</td>";
	dataTable2Data += "<td>" + dataObject.ETHNICITY + "</td>";
	dataTable2Data += "<td>" + dataObject.MAJOR + "</td>";
	dataTable2Data += "<td>" + dataObject.GENDER + "</td>";
	dataTable2Data += "<td class='cityColumn'>" + dataObject.MOST_POPULAR_CITY + "</td>";
	dataTable2Data += "<td class='stateColumn'>" + dataObject.MOST_POPULAR_STATE + "</td>";

	var dataTable2DataRow = document.getElementById("dataRow2");
	dataTable2DataRow.innerHTML = dataTable2Data;
	
	//Modify the display of table cells that are specific to certain region types
	cs441ShibeDoge.regionCellDisplayControl('stateColumn', dataObject.REGION_TYPE, cs441ShibeDoge.NO_REGION_SELECTED);
	cs441ShibeDoge.regionCellDisplayControl('cityColumn', dataObject.REGION_TYPE, cs441ShibeDoge.STATE_SELECTED);

}

/*
 * regionCellDisplayControl
 *
 * Toggles region cells in the summary data table. If the cells with the given class should not exist for the selected
 * region type, hides that column of the table. Otherwise, displays that column in the table.
 * 
 * PARAMS
 * 	className - The class of the cells which we are planning to modify the display of
 * 	regionType - The type of region currently selected by the user
 *  regionTypeRestriction - The encoding for the region with the highest number encoding that should
 *  						display this column in the table
 *  
 */
cs441ShibeDoge.regionCellDisplayControl = function(className, regionType, regionTypeRestriction) {
	var cells = document.getElementsByClassName(className);
	for (var i = 0; i < cells.length; i++) {
		if (regionType > regionTypeRestriction) {
			cells[i].style.display = 'none';
		}
		else {
			cells[i].style.display = 'table-cell';
		}
	}
}
